/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.managers
{
	import flash.display.DisplayObject;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	/**
	 * The StageManager class is used to monitor a DisplayObject's adding to and removal from the display list.
	 * onAdd and onRemoval method callbacks can be provided when subscribing a DisplayObject. 
	 *
	 * @langversion ActionScript 3
	 * @author 		A. Gosman
	 * @since  		14/06/2009
	 */
	public class StageManager
	{
		protected static var DISPLAY_OBJECTS		: Dictionary;
		
		
		public function StageManager()
		{
			throw new IllegalOperationError( "StageManager only provides static methods and can not be instantiated" );
		}
		
		/**
		 * 	Subscribe a DisplayObject with the StageManager so its adding/removing from the display list is monitored and can be linked to callback functions.	
		 * 
		 * 	@param	displayObject		The DisplayObject instance that will be monitored bu the StageManager.
		 * 	@param	onAddCallBack		The function that will be called when the DisplayObject is added to the display list.
		 * 	@param	onRemoveCallBack	The function that will be called when the DisplayObject is removed from the display list.
		 * 	@param	autoUnsubscribe		Defines whether the DisplayObject is automatically unsubscribed from the StageManager when it is removed from the display list.
		 */
		public static function subscribe( displayObject:DisplayObject, onAddCallBack:Function, onRemoveCallBack:Function, autoUnsubscribe:Boolean = false ):void
		{
			if ( !DISPLAY_OBJECTS )
			{
				DISPLAY_OBJECTS 		= new Dictionary( true );
			}
			
			if( StageManager.isSubscribed( displayObject ) )
			{
				return;
			} 
			
			DISPLAY_OBJECTS[ displayObject ] 	= { onAdd: onAddCallBack, onRemove: onRemoveCallBack, autoUnsubscribe: autoUnsubscribe };
			
			if( displayObject.stage )
			{
				displayObject.addEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromToStage, false, 0, true );
			}
			else
			{
				displayObject.addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );
			}
		}
		
		/**
		 * 	Unsubscribed a DisplayObject from the StageManager so its addition/removal to/from the display list is no longer monitored.
		 * 
		 * 	@param	displayObject	The DisplayObject instance to be unsubscribed.
		 */
		public static function unsubscribe( displayObject:DisplayObject ):void
		{
			if( !displayObject )
			{
				throw new Error( "Specify a DisplayObject to unsubscribe from the StageManager" );
			}
			
			if ( !( StageManager.isSubscribed( displayObject ) ) )
			{
				return;
			}
			
			if( displayObject.hasEventListener( Event.ADDED_TO_STAGE ) )
			{
				displayObject.removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
			}
			
			if( displayObject.hasEventListener( Event.REMOVED_FROM_STAGE ) )
			{
				displayObject.removeEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromToStage );
			}
			
			delete DISPLAY_OBJECTS[ displayObject ]; 	
		}
		
		/**
		 * 	Check whether a DisplayObject instance is subscribed with the StageManager or not.
		 * 
		 * 	@param		displayObject		The DisplayObject instance to check for subscription.
		 * 	@return							True when subscribed, false when not subscribed.
		 */
		public static function isSubscribed( displayObject:DisplayObject ):Boolean
		{
			return displayObject in DISPLAY_OBJECTS;
		}
		
		
		protected static function onAddedToStage( event:Event ):void
		{
			var displayObject:DisplayObject 	= event.target as DisplayObject;
			
			displayObject.addEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromToStage, false, 0, true );
			displayObject.removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
			
			// if the displayObject is subscribed and an onAdd callback is defined
			if( StageManager.isSubscribed( displayObject ) && DISPLAY_OBJECTS[ displayObject ].onAdd )
			{
				DISPLAY_OBJECTS[ displayObject ].onAdd( displayObject );
			}
		}
		
		protected static function onRemovedFromToStage( event:Event ):void
		{
			var displayObject:DisplayObject 	= event.target as DisplayObject;

			displayObject.addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );
			displayObject.removeEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromToStage );
			
			if( StageManager.isSubscribed( displayObject ) )
			{
				if( DISPLAY_OBJECTS[ displayObject ].onRemove )
				{
					DISPLAY_OBJECTS[ displayObject ].onRemove( displayObject );
				}
				
				if( DISPLAY_OBJECTS[ displayObject ].autoUnsubscribe )
				{
					StageManager.unsubscribe( displayObject );
				}
			}
		}

	}
}